package com.framework.spring.postprocessors;

import com.framework.spring.annotations.Autowired;
import com.framework.spring.annotations.Component;
import com.framework.spring.annotations.Schedule;
import com.framework.spring.schedule.MyScheduler;

import java.lang.reflect.Method;

/**
 * @author duanzhizhou
 * @date 2025/4/10
 */
@Component
public class ScheduleBeanPostProcessor implements BeanPostProcessor{
    
    @Autowired
    private MyScheduler scheduler;
    
    @Override
    public Object afterInitialization(Object bean, String name) {
        for (Method method : bean.getClass().getDeclaredMethods()) {
            if (method.isAnnotationPresent(Schedule.class)) {
                scheduler.addTask(() -> {
                    try {
                        method.invoke(bean);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }, method.getAnnotation(Schedule.class).delay()
                        , method.getAnnotation(Schedule.class).period());
            }
        }
        return bean;
    }
}
